package com.zhijiang.hospital.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhijiang.hospital.entity.InpatientPerformanceEvaluation;
import com.zhijiang.hospital.entity.dto.InpatientPerformanceEvaluationDto;
import com.zhijiang.hospital.mapper.InpatientPerformanceEvaluationDtoMapper;
import com.zhijiang.hospital.mapper.InpatientPerformanceEvaluationMapper;
import com.zhijiang.hospital.service.InpatientPerformanceEvaluationService;
import com.zhijiang.util.BaseQuery;
import com.zhijiang.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 75936
 * @version 1.0
 * @project zhijiang_hospital-data
 * @description
 * @date 2022-10-25 15:05:52
 */
@Service
@Slf4j
public class InpatientPerformanceEvaluationServiceImpl implements InpatientPerformanceEvaluationService {
    @Autowired
    private InpatientPerformanceEvaluationMapper inpatientPerformanceEvaluationMapper;
    @Autowired
    InpatientPerformanceEvaluationDtoMapper inpatientPerformanceEvaluationDtoMapper;
    ;

    @Override
    public List<InpatientPerformanceEvaluationDto> findAll() {
        return inpatientPerformanceEvaluationMapper.findAll();
    }

    @Override
    public Page<InpatientPerformanceEvaluation> loadPageList(BaseQuery baseQuery) {
        QueryWrapper<InpatientPerformanceEvaluation> inpatientPerformanceEvaluationQueryWrapper = new QueryWrapper<>();
        inpatientPerformanceEvaluationQueryWrapper.
                apply(StrUtil.isNotBlank(baseQuery.getStartTime()),
                        "create_time >= to_date('" + baseQuery.getStartTime() + "','yyyy-mm-dd hh24:mi:ss')").
                apply(StrUtil.isNotBlank(baseQuery.getEndTime()),
                        "create_time <= to_date('" + baseQuery.getEndTime() + "','yyyy-mm-dd hh24:mi:ss')").
                eq(StringUtils.isNotBlank(baseQuery.getDepartment()) && !baseQuery.getDepartment().equals("全部科室"), "dept_Name", baseQuery.getDepartment());
        return inpatientPerformanceEvaluationMapper.selectPage(new Page<>(baseQuery.getPage(), baseQuery.getPageSize()), inpatientPerformanceEvaluationQueryWrapper);

    }

    @Override
    public InpatientPerformanceEvaluation loadOne(String id) {
        return inpatientPerformanceEvaluationMapper.selectById(id);
    }

    @Override
    public List<String> loadDept() {
        List list = inpatientPerformanceEvaluationMapper.selectList(null).stream().map(InpatientPerformanceEvaluation::getDeptName).distinct().collect(Collectors.toList());
        return list;
    }

    @Override
    public List<String> findDddsWork() {
        List<String> list = inpatientPerformanceEvaluationMapper.
                findDddsWork().stream().map
                (InpatientPerformanceEvaluation::getDddsWork).
                collect(Collectors.toList());
        return list;
    }

    @Override
    public List<String> findDddsWorkOfMonth() {
        List<String> list = inpatientPerformanceEvaluationMapper.findDddsWorkOfMonth().
                stream().map(InpatientPerformanceEvaluation::getDddsWork).
                collect(Collectors.toList());
        return list;
    }

    @Override
    public List<String> findDddsWorkOfYear() {
        List<String> list = inpatientPerformanceEvaluationMapper.findDddsWorkOfYear().
                stream().map(InpatientPerformanceEvaluation::getDddsWork).
                collect(Collectors.toList());
        return list;
    }

    @Override
    public List<InpatientPerformanceEvaluationDto> getYearData() {
        DateTime dateTime = DateUtil.beginOfYear(new Date());
        DateTime dateTime1 = DateUtil.endOfYear(new Date());
        String startTime = Convert.toStr(dateTime);
        String endTime = Convert.toStr(dateTime1);
        QueryWrapper<InpatientPerformanceEvaluationDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.
                apply(StrUtil.isNotBlank(startTime),
                        "create_time >= to_date('" + startTime + "','yyyy-mm-dd hh24:mi:ss')").
                apply(StrUtil.isNotBlank(endTime),
                        "create_time <= to_date('" + endTime + "','yyyy-mm-dd hh24:mi:ss')");
        List<InpatientPerformanceEvaluationDto> inpatientPerformanceEvaluations = inpatientPerformanceEvaluationDtoMapper.selectList(queryWrapper);
        return inpatientPerformanceEvaluations;
    }

    @Override
    public List<InpatientPerformanceEvaluationDto> getLastYearData() {
        Date nextYearPreDay = TimeUtil.getNextYearPreDay();
        DateTime dateTime3 = DateUtil.beginOfYear(nextYearPreDay);
        DateTime dateTime4 = DateUtil.endOfYear(nextYearPreDay);
        String startTime = TimeUtil.format(dateTime3);
        String endTime = TimeUtil.format(dateTime4);
        QueryWrapper<InpatientPerformanceEvaluationDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.
                apply(StrUtil.isNotBlank(startTime),
                        "create_time >= to_date('" + startTime + "','yyyy-mm-dd hh24:mi:ss')").
                apply(StrUtil.isNotBlank(endTime),
                        "create_time <= to_date('" + endTime + "','yyyy-mm-dd hh24:mi:ss')");
        List<InpatientPerformanceEvaluationDto> inpatientPerformanceEvaluations = inpatientPerformanceEvaluationDtoMapper.selectList(queryWrapper);
        return inpatientPerformanceEvaluations;
    }


}
